package com.toilet.easyvideofx.manager;

import com.google.gson.Gson;
import com.toilet.easyvideofx.api.request.LoginRequest;
import com.toilet.easyvideofx.api.request.NormalRegisterRequest;
import com.toilet.easyvideofx.api.response.LoginResponse;
import com.toilet.easyvideofx.api.response.RegisterResponse;
import com.toilet.easyvideofx.compent.RequestComponent;

public class LoginManager {

    private static final String LOGIN_URL = "/api/auth/login";
    private static final String REGISTER_URL = "/api/auth/normal_register";

    /**
     * Login to the API
     *
     * @param username 账号名
     * @param password 密码
     */
    public Boolean login(String username, String password) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setPassword(password);
        loginRequest.setUserName(username);
        LoginResponse response = RequestComponent.post(loginRequest, LOGIN_URL, LoginResponse.class);
        // 保存token
        if (response != null) {
            RequestComponent.putExtraHeader(response.getTokenHead(), response.getToken());
        }
        System.out.println(new Gson().toJson(response));
        return true;
    }

    public Boolean register(String username, String password, String confirmPassword) {
        NormalRegisterRequest loginRequest = new NormalRegisterRequest();
        loginRequest.setPassword(password);
        loginRequest.setConfirmPassword(password);
        loginRequest.setUserName(username);
        RegisterResponse response = RequestComponent.post(loginRequest, REGISTER_URL, RegisterResponse.class);
        return response.getRegisterResult();
    }
}
